/**
 * @file dbfilm.c
 * @brief Fichier source du module database.
 *
 * @author Rachid Delory, Mamadou Keita, Florian Bernard
 */

/* *********************************************************************

                       Librairies de database

********************************************************************* */

#include "dbfilm.h"

/* *********************************************************************

                        Fonctions

********************************************************************* */

void recuperation_DFil(Oeuvre *oeuvre, const int id)
{
  if(oeuvre)
    {
      DataBase data;
      ouvrir_Db();
      if(id == 0)
	{
	  /* Récupération de ceux qui n'ont pas de pere */
	  prepare_Db(&data, "select rowid, metrage, dateBlueRay, dateDVD, duree, dateProduction, synopsis from film where rowid not in (select rowid from film)");
	}
      else
	{
	  /* Récupération de ceux qui ont comme pere id */
	  prepare_Db(&data, "select rowid, metrage, dateBlueRay, dateDVD, duree, dateProduction, synopsis from film where rowid = ?");
      	  bindValueInt_Db(&data, 1, id);
	}
      if(execute_Db(&data))
	{
	  Film fil;
	  Date *date = NULL;
	  Temps *tmps = NULL;
	  init_Fil(&fil);
	  setId_Fil(&fil, getValueInt_Db(&data, 0));
	  setMetrage_Fil(&fil, getValueInt_Db(&data, 1));
	  recuperation_DDat(&date, getValueInt_Db(&data, 2));
	  setDateBlueRay_Fil(&fil, date);
	  if(date)
	    detruire_Dat(&date);
	  recuperation_DDat(&date, getValueInt_Db(&data, 3));
	  setDateDvd_Fil(&fil, date);
	  if(date)
	    detruire_Dat(&date);
	  
	  recuperation_DTem(&tmps, getValueInt_Db(&data, 4));
	  setDuree_Fil(&fil, tmps);
	  if(tmps)
	    detruire_Tem(&tmps);
	  recuperation_DDat(&date, getValueInt_Db(&data, 5));
	  setDateProduction_Fil(&fil, date);
	  if(date)
	    detruire_Dat(&date);
	  setSynopsis_Fil(&fil, getValueText_Db(&data, 6));

	  setMedia_Oeu(oeuvre, &fil, copier_Fil);
	  liberer_Fil(&fil);
	}
      finalise_Db(&data);
    }
}

void insertion_DFil(Film *film, const int forUpdate)
{
  if(film)
    {
      DataBase data;
      ouvrir_Db();
      if(getSup_Fil(film) == 1)
	{
	  suppression_DFil(getId_Fil(film));
	  return;
	}
      else if(!forUpdate)
	{
	  /* Insertion */
	  prepare_Db(&data, "insert into film (metrage, dateBlueRay, dateDVD, duree, dateProduction, synopsis, rowid) values (?, ?, ?, ?, ?, ?, ?)");
	}
      else
	{
	  /* Update */
	  if(getMaj_Fil(film) == 1)
	    {
	      prepare_Db(&data, "update film set metrage = ? , dateBlueRay = ?, dateDVD = ?, duree = ?, dateProduction = ?, synopsis = ? where rowid = ?");
	    }
	}
      if(getMaj_Fil(film) == 1 || !forUpdate)
	{
	  bindValueInt_Db(&data, 1, getMetrage_Fil(film));
	  insertion_DDat(getDateBlueRay_Fil(film));
	  bindValueInt_Db(&data, 2, getId_Dat(getDateBlueRay_Fil(film)));
	  insertion_DDat(getDateDvd_Fil(film));
	  bindValueInt_Db(&data, 3, getId_Dat(getDateDvd_Fil(film)));
	  insertion_DTem(getDuree_Fil(film));
	  bindValueInt_Db(&data, 4, getId_Tem(getDuree_Fil(film)));
	  insertion_DDat(getDateProduction_Fil(film));
	  bindValueInt_Db(&data, 5, getId_Dat(getDateProduction_Fil(film)));
	  bindValueText_Db(&data, 6, getSynopsis_Fil(film));
	  bindValueInt_Db(&data, 7, getId_Fil(film));
	  if(execute_Db(&data))
	    {

	    }
	  finalise_Db(&data);
	}
    }
}

void suppression_DFil(const int id)
{
  if(id != 0)
    {
      DataBase data;
      ouvrir_Db();
      prepare_Db(&data, "delete from film where rowid = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
        {
	  
        }
      finalise_Db(&data);
      prepare_Db(&data, "update Oeuvre set id_Film = O where id_Film = ?");
      bindValueInt_Db(&data, 1, id);
      if (execute_Db(&data))
        {

        }
      finalise_Db(&data);
    }
}

void regression_DFil()
{
  Film film;
  Date dat;
  Temps temps;
  init_Fil(&film);

  setMetrage_Fil(&film, LONG_FIL);
  init_Dat(&dat);
  setJour_Dat(&dat, 3);
  setMois_Dat(&dat, 4);
  setAnnee_Dat(&dat, 2011);
  setDateBlueRay_Fil(&film, &dat);
  liberer_Dat(&dat);
  init_Dat(&dat);
  setJour_Dat(&dat, 6);
  setMois_Dat(&dat, 8);
  setAnnee_Dat(&dat, 2001);
  setDateDvd_Fil(&film, &dat);
  liberer_Dat(&dat);
  init_Tem(&temps);
  setMinute_Tem(&temps, 59);
  setDuree_Fil(&film, &temps);
  liberer_Tem(&temps);
  init_Dat(&dat);
  setAnnee_Dat(&dat, 2009);
  setDateProduction_Fil(&film, &dat);
  setSynopsis_Fil(&film, "Resume de test");
  liberer_Dat(&dat);
  setId_Fil(&film, 2);
  insertion_DFil(&film, 0);
  liberer_Fil(&film);
}
